<template>
  <el-dialog :title=" editTab ? '修改' :  '新增'" :close-on-click-modal="false" :visible.sync="visible" width="800px" top="8vh">
    <el-container>
      <el-main style="max-height:60vh;">
        <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
          <el-row v-for="(item,index) in dataForm.dataList" :key="index" class="groupBox">
            <el-col :span="12" :xs="24">
              <el-form-item label="分值所属组" :prop="'dataList['+index+'].fzSsz'" :rules="[
          { required: true, message: '分值所属组不能为空', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }]">
                <el-select v-model="item.fzSsz">
                  <el-option v-for="(item,index) in  cpPfgzFzOptions " :key="index" :label="item.label" :value="item.label">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" :xs="24">
              <el-form-item label="分值名称" :prop="'dataList['+index+'].fzMc'" :rules="[
           { required: true, message: '分值名称不能为空', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
          ]">
                <el-input v-model="item.fzMc" placeholder="分值名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :xs="24">
              <el-form-item label="分值分数" :prop="'dataList['+index+'].fzFs'" :rules="[
          { required: true, message: '分值分数不能为空', trigger: 'blur' },
          {pattern: /^(0|[1-9]\d?|100)$/, message: '范围在0-100', trigger: 'blur'}
          ]">
                <el-input-number   v-model="item.fzFs" placeholder="分值分数"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12" :xs="24">

              <el-form-item label="分值排序" :prop="'dataList['+index+'].fzPx'" :rules="[
          { required: true, message: '分值排序不能为空', trigger: 'blur' },
          ]">
                <div class="flex justifyContentSpaceBetween alignItemsCenter">
                  <el-input-number :min="1" :max="10000" v-model="item.fzPx" placeholder="分值排序"></el-input-number>
                  <div v-if="!editTab">
                    <el-button @click="removeItem(index)" v-if="dataForm.dataList.length>1" icon="el-icon-minus">
                    </el-button>
                  </div>
                </div>
              </el-form-item>
            </el-col>
            <div v-if="!editTab">
              <el-button @click="addItem()" v-if="index==dataForm.dataList.length-1" style="float: right;" icon="el-icon-plus">
              </el-button>
            </div>
          </el-row>
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
/**
 * 绩效考核_测评_评分明细表单
 * @author zgc
 * @date 2021-06-16 10:06:44
 */
import {
  getObj,
  addObj,
  putObj
} from '@/api/ceping/cepinggzmxb'
import {
  remote as remoteDict
} from '@/api/admin/dict'
// import {mapGetters} from '_vuex@3.6.2@vuex';
export default {
  data() {
    return {
      gzId: '',
      visible: false,
      canSubmit: false,
      editTab: false,
      dataForm: {
        dataList: []
      },
      dataRule: {
        gzId: [{
          required: true,
          message: '所属规则不能为空',
          trigger: 'blur'
        }],
        fzSsz: [{
          required: true,
          message: '分值所属组不能为空',
          trigger: 'blur'
        },
        {
          min: 1,
          max: 30,
          message: '长度在 1 到 30 个字符',
          trigger: 'blur'
        }
        ],
        fzMc: [{
          required: true,
          message: '分值名称不能为空',
          trigger: 'blur'
        },
        {
          min: 1,
          max: 30,
          message: '长度在 1 到 30 个字符',
          trigger: 'blur'
        }
        ],
        fzFs: [{
          required: true,
          message: '分值分数不能为空',
          trigger: 'blur'
        },
          // { min: 1, max: 10, message: '长度在 1 到 10 位', trigger: 'blur' }
          //{ type:'number', message: '分值分数必须为数字', trigger: 'blur' },
          // {pattern:'^\d+(\.\d+)?$',message: '范围在0-100', trigger: 'blur'}
        ],
        fzPx: [{
          required: true,
          message: '分值排序不能为空',
          trigger: 'blur'
        },
          // { min: 1, max: 10, message: '长度在 1 到 10 位', trigger: 'blur' }
          // { type:'number', message: '分值排序必须为数字', trigger: 'blur' },
          // {pattern:'^\d+(\.\d+)?$',message: '范围在0-100', trigger: 'blur'}
        ]
      },
      cpPfgzFzOptions: []
    }
  },
  created() {
    remoteDict("cp_pfgz_fz").then(response => {
      this.cpPfgzFzOptions = response.data.data
     //console.log(this.cpPfgzFzOptions)
    })
  },
  // computed: {
  //   ...mapGetters(['permissions'])
  // },
  methods: {
    init(ruleId, id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.editTab = false;
      this.gzId = ruleId;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();

        if (this.dataForm.id) {
          this.dataForm.dataList = []
          getObj(this.dataForm.id).then(response => {
            this.dataForm.dataList.push(response.data.data);
            this.editTab = true;
          })
        } else {
          if (ruleId) {
            this.setInitNone(ruleId);
          }
        }
      })

    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm.dataList[0]).then(data => {
              this.$notify.closeAll();
              this.$notify.success('修改成功');
              this.visible = false;
              this.dataForm.dataList = [];
              this.$emit('getSubList', this.dataForm);
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm.dataList).then(data => {
              this.$notify.closeAll();
              this.$notify.success('添加成功');
              this.visible = false;
              this.$emit('getSubList', this.dataForm);
              this.dataForm.dataList = [];
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //添加数据项
    addItem() {
      this.dataForm.dataList.push({
        gzId: this.gzId,
        fzMc: '',
        fzFs: '',
        fzPx: '',
      });
    },
    //移除数据项
    removeItem(index) {
      this.dataForm.dataList.splice(index, 1);
    },
    //初始化赋值
    setInitNone(gzId) {
      this.dataForm = {
        dataList: [{
          gzId: gzId,
          fzMc: '',
          fzFs: '',
          fzPx: '',
        }],
      }
    },
  }
}

</script>
